package com.yzmoney.wechat.action;

import java.util.HashMap;
import java.util.Map;

import com.yzmoney.common.constant.Constants;
import com.yzmoney.common.model.JsonResult;
import com.yzmoney.common.service.StaticService;
import com.yzmoney.common.util.JDKStackTrace;
import com.yzmoney.common.util.JsonUtils;
import com.yzmoney.wechat.service.WechatContactsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 企业号通讯录中的用户操作
 *
 * @author DOVE
 */
@Controller
@RequestMapping("/user")
public class ContactsAction {
    private static Logger logger = Logger.getLogger(ContactsAction.class);

    @Resource
    private WechatContactsService wechatContactsService;
    @Resource
    private StaticService staticService;

    /**
     * 通讯录创建用户
     *
     * @param request
     * @param response
     * @return json格式代码
     */
    @RequestMapping("/create")
    @ResponseBody
    public JsonResult createUser(HttpServletRequest request, HttpServletResponse response) {
        logger.info(Constants.LOGPRIFIX + "---create begin");
        String tpMsg = request.getParameter("tpMsg");
        JsonResult jsonResult = new JsonResult();
        if (StringUtils.isNotEmpty(tpMsg)) {
            logger.info(Constants.LOGPRIFIX + "----tpMsg=" + tpMsg);
            jsonResult = wechatContactsService.userOperate(tpMsg, "create");
            return jsonResult;
        }
        jsonResult.setCode(Constants.CODE_FAILURE);
        jsonResult.setMsg("请传入有效的模版");
        return jsonResult;
    }

    /**
     * 修改通讯录用户
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public JsonResult updaeUser(HttpServletRequest request, HttpServletResponse response) {
        logger.info(Constants.LOGPRIFIX + "---update begin");
        String tpMsg = request.getParameter("tpMsg");
        JsonResult jsonResult = new JsonResult();
        logger.info(Constants.LOGPRIFIX + "----tpMsg=" + tpMsg);
        try {
            if (StringUtils.isEmpty(tpMsg)) {
                jsonResult.setCode(Constants.CODE_FAILURE);
                jsonResult.setMsg("参数为空，请联系管理员");
                return jsonResult;
            }
            Map<String, String> map = JsonUtils.toMap(tpMsg);
            if (map == null) {
                jsonResult.setCode(Constants.CODE_FAILURE);
                jsonResult.setMsg("解析数据出错，请联系管理员");
                return jsonResult;
            }
            String userId = map.get("userid");
            logger.info(Constants.LOGPRIFIX + " userId:" + userId);
            if (StringUtils.isEmpty(userId)) {
                jsonResult.setCode(Constants.CODE_FAILURE);
                jsonResult.setMsg("获取用户id出错，请联系管理员");
                return jsonResult;
            }
            jsonResult = wechatContactsService.getUser(userId, "get");
            logger.info(Constants.LOGPRIFIX + "get user result code ： " + jsonResult.getCode());
            if (jsonResult.getCode() == 60111) {
                logger.info(Constants.LOGPRIFIX + "get user null to create user tpMsg：" + tpMsg);
                jsonResult = wechatContactsService.userOperate(tpMsg, "create");
            } else if (jsonResult.getCode() == 0) {
                logger.info(Constants.LOGPRIFIX + "get user success to update user tpMsg：" + tpMsg);
                jsonResult = wechatContactsService.userOperate(tpMsg, "update");
            }
            return jsonResult;
        } catch (Exception e) {
            logger.warn(JDKStackTrace.getJDKStrack(e));
            jsonResult.setCode(Constants.CODE_FAILURE);
            jsonResult.setMsg("解析数据出错，请联系管理员");
            return jsonResult;
        }
    }

    /**
     * 删除通讯录用户
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult deleteUser(HttpServletRequest request, HttpServletResponse response) {
        logger.info(Constants.LOGPRIFIX + "---delete begin");
        String userId = request.getParameter("userId");
        JsonResult jsonResult = new JsonResult();
        if (StringUtils.isNotEmpty(userId)) {
            logger.info(Constants.LOGPRIFIX + "----userId=" + userId);
            jsonResult = wechatContactsService.userDelete(userId, "delete");
            return jsonResult;
        }
        jsonResult.setCode(Constants.CODE_FAILURE);
        jsonResult.setMsg("请传入有效的模版");
        return jsonResult;
    }
}
